export function getPreviousDays (m) {
    let daysArr = []

    function getNDay (n) {
        const now = new Date()
        const date = new Date(now.getTime() - n * 24 * 3600 * 1000)
        const year = date.getFullYear()
        const month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
        const day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
        // const dayStr = year + "-" + month + "-" + day;
        const dayStr = month + '-' + day
        return dayStr
    }

    for (let i = m; i > 0; i--) {
        const value = getNDay(i)
        daysArr.push(value)
    }
    return daysArr
}

export function getTotalTime (arr) {
    let idleTime = 0
    let runningTime = 0
    let downTime = 0
    let inactiveTime = 0
    for (let i of arr) {
        switch (i.status) {
            case 0:
                idleTime += parseFloat(i.time)
                break
            case 1:
                runningTime += parseFloat(i.time)
                break
            case 2:
                downTime += parseFloat(i.time)
                break
            case 3:
                inactiveTime += parseFloat(i.time)
                break
        }
    }
    return [
        {status: 0, time: idleTime},
        {status: 1, time: runningTime},
        {status: 2, time: downTime},
        {status: 3, time: inactiveTime},
    ]
}

export function formatJSON (jsonStr) {
    const json = jsonStr.replace(/\'/g, '\"')
    return json
}

export function getDateAndTime () {
    const date = new Date()
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()
    month = month > 9 ? month : `0${month}`
    day = day > 9 ? day : `0${day}`
    hour = hour > 9 ? hour : `0${hour}`
    minute = minute > 9 ? minute : `0${minute}`
    second = second > 9 ? second : `0${second}`
    return {
        date: `${year}/${month}/${day}`,
        time: `${hour}:${minute}:${second}`,
    }
}

export function setCookie (json) {
    const key = Object.keys(json)[0]//JSON只能有一个key
    document.cookie = `${key}=${encodeURIComponent(json[key])};max-age=${7 * 24 * 60 * 60}`
}

export function getCookie (name) {
    let arr
    const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
    if (arr = document.cookie.match(reg)) {
        return decodeURIComponent(arr[2])
    } else {
        return null
    }
}

export function delCookie (name) {
    var value = this.getCookie(name)
    if (value) {
        document.cookie = name + '=' + value + ';max-age=0'
    }
}

export function uuid(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [], i;
    radix = radix || chars.length;

    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
        // rfc4122, version 4 form
        var r;

        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random()*16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}